#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=510;
char numset[N][N];
int weightset[N][N];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};

typedef struct node
{
	int Weight;
	int x,y;
	bool operator <(const node& other)const
	{
		return Weight>other.Weight;
	}
}node;

int main()
{
	int n,m;
	while(cin>>n>>m,n&m)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>numset[i][j];
			}
		}

		int x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;

		memset(weightset,0x3f,sizeof weightset);
		//dijkstra
		{
			priority_queue<node> pq;
			pq.push({0,x1,y1});
			weightset[x1][y1]=0;
			while(pq.size())
			{
				auto topnum=pq.top();pq.pop();
				int maxval=topnum.Weight;
				int x=topnum.x;
				int y=topnum.y;
				if(maxval>weightset[x][y]) continue;

				if(x==x2&&y==y2)
				{
					cout<<maxval<<endl;
					break;
				}

				for(int i=0;i<4;i++)
				{
					int a=x+dx[i];
					int b=y+dy[i];
					if(a<n&&b<m&&a>=0&&b>=0)
					{
						int newmax=maxval+
						(numset[a][b]==numset[x][y]?0:1);

						if(newmax<weightset[a][b])
						{
							weightset[a][b]=newmax;
							pq.push({newmax,a,b});
						}
					}
				}
			}
		}
	}
}